﻿writeCookie("userId", "", 1);
writeCookie("RoleType", "", 1);
writeCookie("CampId", "", 1);
$(document).ready(function () {
    getListTimeZone();
    // Get Info User Login
    $.ajax({
        url: gb_servicePath + '/authenticateduser/ID/' + getURLParameter('userID') + '.json',
        crossDomain: "true",
        type: "GET",
        contentType: 'application/json; charset=utf-8',
        traditional: true,
        data: "",
        dataType: 'json',
        success: function (data) {
            if (data.StatusCode == 200) {
                var admin = data.ReturnData;

                $('#lblUsername').html(admin.UserName);
                $('#lblFName').html(admin.FirstName);
                $('#lblLName').html(admin.LastName);
                $('#txtEmail').val(admin.EmailAddress);
                $('#txtPhone').val(admin.PhoneNumber);
                $('#ddlTimeZone option[name=' + admin.TimeZoneID + ']').attr("selected", "selected");
                $('#timezone_selection .customStyleSelectBoxInner').html($('#ddlTimeZone option[name=' + admin.TimeZoneID + ']').val());
                $('#lblEnterprise').html(admin.CompanyBrandInfo.CompanyInfo.Name);
                $('#lblBrandName').html(admin.CompanyBrandInfo.BrandInfo.Name);
                $('#lblBrandDescription').html(admin.CompanyBrandInfo.BrandInfo.Desc);

                $('#val_email').html(admin.EmailAddress);
                $('#val_phone').html(admin.PhoneNumber);
                $('#val_timezone').html($('#ddlTimeZone option:selected').val());

                $('.camp_content_main').remove();
                $.each(admin.CampaignRoleCountryInfo, function (i, val) {
                    var c = $(".camp_content_temp").clone();
                    $(c).removeClass("hide").removeClass('camp_content_temp').addClass('camp_content_main');
                    $(c).find(".td_campName").html(val.CampaignRoleInfo.CampaignInfo.Name);
                    $(c).find(".td_role").html(val.CampaignRoleInfo.RoleInfo.Name);
                    var countries = "";
                    $.each(val.CountryList, function (i, val_country) {
                        countries += val_country.Name + ", ";
                    });
                    $(c).find(".td_country").html(countries.substring(0, countries.length - 2));
                    $(".camp").append(c);
                });
            }
        },
        error: function (jqXHR, textStatus, xerror) {
            if (isBug) {
                alert("Error in /authenticateduser/ID/' + userId + '.json ->" + xerror);
            }
        }
    });
    //Get Details User by id
    // Get List Time Zone
    function getListTimeZone() {
        $.getJSON(gb_servicePath + '/countrypreference/list.json',
                function (json1) {
                    if (json1.StatusCode == 200) {
                        $.each(json1.ReturnData, function (i, val) {
                            if (i == 0) {
                                var o = $("<option selected='selected' name='" + val.Timezone.Id + "' id='tz" + val.Timezone.Id + "' value='" + val.Timezone.Name + "' >" + val.Timezone.Name + "</option>");
                                $(".select_time").find(".customStyleSelectBoxInner").html(val.Timezone.Name);
                            } else {
                                var o = $("<option name='" + val.Timezone.Id + "' id='tz" + val.Timezone.Id + "' value='" + val.Timezone.Name + "' >" + val.Timezone.Name + "</option>");
                            }
                            if ($("#tz" + val.Timezone.Id).length == 0) {
                                $("#timezone_selection").find("select").append(o);
                            }
                        });
                    }
                });
    }
    function ActiveAccount(user, pass) {
        var Login = new Object();
        Login.username = user;
        Login.password = pass;

        $.ajax({
            url: gb_servicePath + "authenticateduser/active/" + getURLParameter('userID') + ".json",
            crossDomain: "true",
            type: "POST",
            contentType: 'application/json; charset=utf-8',
            traditional: true,
            data: JSON.stringify(Login),
            "dataType": 'json',
            success: function (data) {
                window.location.replace('RegistrationSuccess');
            },
            error: function (jqXHR, textStatus) {
                alert("Error");
                alert(Login);
            }

        });
    }
    $('#btn_Save').click(function () {
        var user = $('#txtUserName').val();
        var newpass = $('#txtNewPass').val();
        var repass = $('#txtRePass').val();
        if (user == '') {
            if (newpass == '') {
                $('#title_newpass').css('color', '#ff5050');
            }
            if (repass == '') {
                $('#title_repass').css('color', '#ff5050');
            }
            $('#title_User').css('color', '#ff5050');
            $('#txtUserName').val('');
            $('#txtUserName').focus();
            $('#warning').html('Please complete the required fields');
            $('#warning').css('color', '#ff5050');
            return false;
        }
        if (newpass == '') {
            $('#title_newpass').css('color', '#ff5050');
            $('#txtRePass').val('');
            $('#txtNewPass').val('');
            $('#txtNewPass').focus();
            $('#warning').html('Please complete the required fields');
            $('#warning').css('color', '#ff5050');
            if (repass == '') {
                $('#title_repass').css('color', '#ff5050');
                $('#txtRePass').val('');
                $('#warning').html('Please complete the required fields');
                $('#warning').css('color', '#ff5050');
                return false;
            }
        }
        if (repass == '') {
            $('#title_repass').css('color', '#ff5050');
            $('#txtRePass').val('');
            $('#txtRePass').focus();
            $('#warning').html('Please complete the required fields');
            $('#warning').css('color', '#ff5050');
            return false;
        }

        if (newpass.length < 6) {
            $('#title_newpass').css('color', '#ff5050');
            $('#title_repass').css('color', '#ff5050');
            $('#txtRePass').val('');
            $('#txtNewPass').val('');
            $('#txtNewPass').focus();
            $('#warning').html('Password length must be greater than 6 characters');
            $('#warning').css('color', '#ff5050');
            return false;
        }
        else {
            if (repass != newpass) {
                $('#title_newpass').css('color', '#ff5050');
                $('#title_repass').css('color', '#ff5050');
                $('#txtRePass').val('');
                $('#txtRePass').focus();
                $('#warning').html('Passwords do not match');
                $('#warning').css('color', '#ff5050');
                return false;
            }
        }
        ActiveAccount(user, newpass);
        
    });

});
